package ch.bfh.cmdh.thinkerforge.weatherstation.controller;

import java.io.IOException;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Observable;

import com.tinkerforge.AlreadyConnectedException;
import com.tinkerforge.BrickStepper.NewStateListener;
import com.tinkerforge.NotConnectedException;
import com.tinkerforge.TimeoutException;

import ch.bfh.cmdh.thinkerforge.weatherstation.model.ambientlight.AmbientLightSensor;
import ch.bfh.cmdh.thinkerforge.weatherstation.model.humidity.HumiditySensor;
import ch.bfh.cmdh.thinkerforge.weatherstation.model.temperature.TemperatureSensor;
import ch.bfh.cmdh.thinkerforge.weatherstation.view.display.DisplayBricklet;

public class WeatherStationController extends Observable{

//    public static final String HOST = "192.168.1.65";
    public static final String HOST = "192.168.43.65";
    public static final int PORT = 4223;
    public static final int CALLBACK_PERIOD = 500;
    
    private AmbientLightSensor ambientLightSensor;
    private TemperatureSensor temperatureSensor;
    private HumiditySensor humiditySensor;
    private DisplayBricklet displayBricklet;
    
    public WeatherStationController(){
        this.ambientLightSensor = new AmbientLightSensor(this);
        this.temperatureSensor = new TemperatureSensor(this);
        this.humiditySensor = new HumiditySensor(this);
        this.displayBricklet = new DisplayBricklet(this);
        addObserver(displayBricklet);
        try {
            this.ambientLightSensor.connect();
            System.out.println("AmbientLightConnected");            
            this.temperatureSensor.connect();
            System.out.println("TemperatureConnected");            
            this.humiditySensor.connect();
            System.out.println("HumidityConnected");            
        } catch (UnknownHostException e) {
            System.out.println("UnknownHostException");            
        } catch (AlreadyConnectedException e) {
            System.out.println("AlreadyConnectedException");            
        } catch (IOException e) {
            System.out.println("IOException");            
        } catch (TimeoutException e) {
            System.out.println("TimeoutException");            
            e.printStackTrace();
        } catch (NotConnectedException e) {
            System.out.println("NotConnectedException");            
        }
    }
    
    
    public void disconnectAllBricks(){
        try {
            this.ambientLightSensor.disconnect();
            this.temperatureSensor.disconnect();
            this.humiditySensor.disconnect();
            this.displayBricklet.disconnect();
            System.out.println("Disconnected");
        } catch (NotConnectedException e) {
            System.out.println("Not-Disconnected");            
        }
    }
    
    public void updateGUI(String key, int value){
        HashMap<String, Integer> map = new HashMap<String, Integer>();
        map.put(key, value);
        setChanged();
        notifyObservers(map);
        clearChanged();
    }
}
